bitmasks brute force dp greedy *1600

Please click on ads to support us..

Python Code:

n,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
for i in range(512):
    flag1 = 0
    for ai in a:
        flag2 = 0
        for bi in b:
            if (ai & bi)|i == i:
                flag2 = 1
                break
        if flag2==0:
            flag1 = 1
            break
    if flag1==0:
        print(i)
        break

C++ Code:

// codex by freakingflux
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define fast()                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define loop(i, ini, n) for (ll i = ini; i < (ll)n; i++)
#define pool(i, n, last) for (ll i = (ll)n; i > last; i--)
#define all(x) x.begin(), x.end()
#define tc     \
    ll tt;     \
    cin >> tt; \
    while (tt--)
#define inf INT_MAX
#define ninf INT_MIN
#define pb push_back
#define po pop_back
#define sz size()
#define ed end()
#define be begin()
#define mi(x) min_element(all(x))
#define mx(x) max_element(all(x))
#define am accumulate
#define rv(x) reverse(all(x))
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define vl vector<ll>
#define pl pair<ll, ll>
#define vll vector<vector<ll>>
#define vp vector<pl>
#define ml map<ll, ll>
#define sl set<ll>
#define msl multiset<ll>
#define mml multimap<ll, ll>
#define pi 3.1415926536
#define read  \
    ll n;     \
    cin >> n; \
    vl v(n);  \
    loop(i, 0, n) cin >> v[i];
ll mod = 1e9 + 7;

void rotate(vl &v, ll k)
{
    ll n = v.sz;
    k = k % n;
    reverse(v.be, v.be + k);
    reverse(v.be + k, v.ed);
    rv(v);
}

bool is_prime(ll n)
{
    // T.C sqrt(n)
    if (n == 0 || n == 1)
        return false;

    for (ll i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }

    return true;
}

vl prime_upto_n(ll n)
{
    // T.C n*log(log(n))
    vector<ll> isPrime(n + 1, 1), lp(n + 1, 0), hp(n + 1, 0);
    isPrime[0] = isPrime[1] = 0;

    for (ll i = 2; i <= n; i++)
    {
        if (isPrime[i])
        {
            lp[i] = hp[i] = i;
            for (ll x = i * i; x <= n; x += i)
            {
                isPrime[x] = 0;
                hp[x] = i;
                if (lp[x] == 0)
                    lp[x] = i;
            }
        }
    }

    return isPrime;
}

sl allFactors(ll n)
{
    // T.C sqrt(n)
    sl factors;
    for (ll i = 1; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            factors.insert(i);
            if (n / i != i)
                factors.insert(n / i);
        }
    }

    return factors;
}

ml allPrimefactors(ll n)
{
    // T.C sqrt(n)
    ml primeFactors;
    for (ll i = 2; i * i <= n; i++)
    {
        while (n % i == 0)
        {
            primeFactors[i]++;
            n /= i;
        }
    }
    if (n > 1)
        primeFactors[n]++;

    return primeFactors;
}

pair<double, ll> sumCnt_of_divisors(ll n)
{
    // T.C sqrt(n)
    ll v = 1;
    double sum = 1;
    ml x = allPrimefactors(n);

    for (auto x : x)
    {
        v *= (x.se + 1);
        sum *= ((pow(x.fi, x.se + 1) - 1) / (x.fi - 1));
    }

    return {sum, v};
}

ll binpow(ll x, ll b)
{
    // T.C log(n)
    if (b == 0)
        return 1;

    ll ans = binpow(x, b / 2);

    if (b % 2)
        return ((ans % mod * ans % mod) % mod * x % mod) % mod;
    else
        return (ans % mod * ans % mod) % mod;
}

ll gcd(ll x, ll b)
{
    // T.C log(min(x, b));
    while (b)
    {
        x %= b;
        swap(x, b);
    }

    return x;
}

void setIO(string s)
{
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

ll msb_finder(ll n)
{
    ll k = __builtin_clzll(n);
    return 1 << (64 - k);
}

bool check(ll a, ll b, ll ha, ll hb)
{
    if (a == 0 && ha == 0)
        return false;
    if (ha == 0 && hb == 0)
        return false;
    if (b == 0 && hb == 0)
        return false;
    if (a == 0 && b == 0)
        return false;
    return true;
}

void solve()
{
    ll n, m;
    cin>>n>>m;
    vl a(n);
    loop(i,0,n) cin>>a[i];
    set<ll>b;
    while(m--){
        ll x;
        cin>>x;
        b.insert(x);
    }

    ll ans=inf;
    loop(i,0,(1<<9)){
        ll cnt=0;
        loop(j,0,n){
            ll flag=0;
            for(auto x:b){
                if(((a[j]&x)|i)==i){
                    flag=1;
                    break;
                }
            }

            if(!flag) break;
            else cnt++;
        }
        if(cnt==n) ans=min(ans, i);
    }

    cout<<ans<<"\n";
}

int main()
{
    // setIO("notlast");
    fast();
    //tc
    {
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

1728C - Digital Logarithm
1728D - Letter Picking
792B - Counting-out Rhyme
1195A - Drinks Choosing
5D - Follow Traffic Rules
1272A - Three Friends
1632D - New Year Concert
1400D - Zigzags
716C - Plus and Square Root
412A - Poster
844B - Rectangles
1591A - Life of a Flower
1398C - Good Subarrays
629A - Far Relative’s Birthday Cake
1166A - Silent Classroom
1000B - Light It Up
218B - Airport
1463B - Find The Array
1538C - Number of Pairs
621B - Wet Shark and Bishops
476B - Dreamoon and WiFi
152C - Pocket Book
1681D - Required Length
1725D - Deducing Sortability
1501A - Alexey and Train
721B - Passwords
1263D - Secret Passwords
1371B - Magical Calendar
1726E - Almost Perfect
1360C - Similar Pairs